package com.carson.undergo.ui.fragment

import android.annotation.SuppressLint
import android.app.Activity.RESULT_OK
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.carson.undergo.R
import com.carson.undergo.base.ApiService
import com.carson.undergo.databinding.FragmentHomeBinding
import com.carson.undergo.ui.activity.home.CanSearchListUI
import com.carson.undergo.ui.activity.home.CartDetailUI
import com.carson.undergo.ui.activity.home.ChooseCityUI
import com.carson.undergo.ui.activity.home.DialogShowUI
import com.carson.undergo.ui.adapter.OpenCloseAdapter
import com.carson.undergo.utils.bean.WanAndroidListBean
import com.model.common.base.fragment.BaseFragment
import com.model.common.utils.always.ToastUtils
import com.model.common.utils.http.RetrofitHelper
import com.model.common.utils.http.back.HttpError
import com.model.common.utils.http.back.NetWorkCallback
import retrofit2.Call

/**
 * Create by Carson on 2021/12/22.
 * HomeFragment---首页
 */
class HomeFragment : BaseFragment<FragmentHomeBinding>() {

    private var adapter: OpenCloseAdapter? = null
    private var mIsShowOnlyThree = true//是否只展示三条数据
    private val infoList = ArrayList<String>()

    override fun getViewBinding(inflater: LayoutInflater, viewGroup: ViewGroup):
            FragmentHomeBinding = FragmentHomeBinding.inflate(inflater, viewGroup, false)

    override fun initViewsAndEvents() {
        initAdapter()
    }

    private fun initAdapter() {
        infoList.add("retrofit进行网络请求框架封装")
        infoList.add("Android仿饿了么购物车功能")
        infoList.add("Android仿IOS弹框")
        infoList.add("Android仿微信好友列表检索功能")
        infoList.add("自定义三级联动列表选择省市区")
        adapter = OpenCloseAdapter()
        adapter?.setNewInstance(infoList)
        if (infoList.size > 3) {
            adapter?.setShowOnlyThree(mIsShowOnlyThree)
            binding.layoutShowAll.visibility = View.VISIBLE
            binding.tvShowAll.text = "展开全部"
            binding.tvShowAll
                .setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.arrow_down, 0)
        } else {
            binding.layoutShowAll.visibility = View.GONE
        }

        val layoutManager = LinearLayoutManager(mContext)
        layoutManager.isSmoothScrollbarEnabled = true
        binding.recyclerView.layoutManager = layoutManager
        binding.recyclerView.setHasFixedSize(true)
        binding.recyclerView.isNestedScrollingEnabled = false
        binding.recyclerView.adapter = adapter

        //查看adapter条目详情
        adapter?.setOnItemClickListener { _, _, position ->
            when (position) {
                //retrofit进行网络请求框架封装
                0 -> getWxArticle(1)
                //Android仿饿了么购物车功能
                1 -> go(CartDetailUI::class.java)
                //Android仿IOS弹框
                2 -> go(DialogShowUI::class.java)
                //Android仿微信好友列表检索功能
                3 -> getWxArticle(0)
                //自定义三级联动列表选择省市区
                4 -> go(ChooseCityUI::class.java)
            }
        }
    }

    /**
     * 获取玩android公众号列表
     */
    private fun getWxArticle(isRetrofit: Int) {
        RetrofitHelper.create(ApiService::class.java)
            .getWxArticleList()
            .enqueue(this, object : NetWorkCallback<ArrayList<WanAndroidListBean>>(mContext) {

                override fun onError(
                    call: Call<ArrayList<WanAndroidListBean>>?,
                    error: HttpError?
                ) {
                    ToastUtils.showMessage(mContext, error?.errorMessage)
                }

                override fun onSuccess(
                    call: Call<ArrayList<WanAndroidListBean>>?,
                    response: ArrayList<WanAndroidListBean>?
                ) {
                    if (isRetrofit == 1) {
                        ToastUtils.showMessage(mContext, "玩android公众号列表")
                    } else {
                        val bundle = Bundle()
                        bundle.putSerializable("list", response)
                        goForResult(CanSearchListUI::class.java, bundle, 0x10)
                    }
                }
            })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK && requestCode == 0x10) {
            ToastUtils.showMessage(mContext, data?.getStringExtra("name"))
        }
    }

    @SuppressLint("UseCompatLoadingForDrawables")
    override fun addListener() {
        super.addListener()
        //展开---收起
        binding.layoutShowAll.setOnClickListener {
            mIsShowOnlyThree = !mIsShowOnlyThree
            adapter?.setShowOnlyThree(mIsShowOnlyThree)
            //改变箭头方向
            val drawable = resources.getDrawable(
                if (mIsShowOnlyThree) {
                    R.drawable.arrow_down
                } else {
                    R.drawable.arrow_up
                }
            )
            drawable.setBounds(0, 0, drawable.minimumWidth, drawable.minimumHeight)
            binding.tvShowAll.setCompoundDrawables(null, null, drawable, null)
            binding.tvShowAll.text = if (mIsShowOnlyThree) "展开全部" else "收起"
        }
    }
}